﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace RealEstateCenter
{
    public partial class EditAgency : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            new login_check("estate_center");
            if (!Page.IsPostBack)
            {
                agencyID.Text = Request.QueryString["id"];
                Session["edit_agency_id"] = Request.QueryString["id"];
                string connectionString;
                SqlConnection sqlconnect;
                connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|RealEstateCenter.mdf;Integrated Security=True;User Instance=True";
                sqlconnect = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand("select * from Agency where agency_id='" + Request.QueryString["id"] + "'", sqlconnect);
                sqlconnect.Open();
                SqlDataReader dtrReader = cmd.ExecuteReader();
                if (dtrReader.Read())
                {
                    agency_username.Text = dtrReader["agency_username"].ToString();
                    agency_password.Text = dtrReader["agency_password"].ToString();
                    agency_name.Text = dtrReader["agency_name"].ToString();
                    agency_tel_no.Text = dtrReader["agency_tel_no"].ToString();
                    agency_address.Text = dtrReader["agency_address"].ToString();
                    agency_mail.Text = dtrReader["agency_mail"].ToString();
                    agency_city.SelectedValue = dtrReader["agency_city"].ToString();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string edit_agency_id = Session["edit_agency_id"].ToString();
            string connectionString;
            SqlConnection sqlconnect;
            connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|RealEstateCenter.mdf;Integrated Security=True;User Instance=True";
            sqlconnect = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("update Agency set agency_username='" + agency_username.Text + "', agency_password='" + agency_password.Text + "', agency_name='" + agency_name.Text + "', agency_tel_no='" + agency_tel_no.Text + "',agency_address='" + agency_address.Text + "', agency_mail='" + agency_mail.Text + "',agency_city='" + agency_city.Text + "' where agency_id=" + Session["edit_agency_id"].ToString(), sqlconnect);
            sqlconnect.Open();
            int kontrol = cmd.ExecuteNonQuery();

            if (kontrol == 1)
            {
                Response.Redirect("ListAgencies.aspx");
            }
        }
    }
}